package io.gitee.chearnee.fw.common.utils;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import lombok.experimental.UtilityClass;

import javax.crypto.SecretKey;
import java.util.Base64;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 12/15/2022 2:35 PM
 */
@UtilityClass
public class AesUtil {

    private static byte[] getKey(String aesKey) {
        return Base64.getDecoder().decode(aesKey);
    }

    public static String decode(String base64Key, String data) {
        return SecureUtil.aes(getKey(base64Key)).decryptStr(data);
    }

    public static String encode(String base64Key, String data) {
        return SecureUtil.aes(getKey(base64Key)).encryptBase64(data);
    }


    public static void main(String[] args) {
        String aesKey = "kFe1R9pTTFvphLgNzbjOYUpr0OkYeV7GurYvgriMZkc=";
        String encode = encode(aesKey, "12345");
        String decode = decode(aesKey, encode);
        SecretKey secretKey = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue(), 256);
        byte[] encoded = secretKey.getEncoded();
        System.out.println(Base64.getEncoder().encodeToString(encoded));
    }
}
